/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.gis.eemc.service;

import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.net.URL;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

import org.richfaces.model.UploadedFile;

import net.gis.eemc.dao.DisciplineDAO;
import net.gis.eemc.dao.DisciplineDAOImpl;
import net.gis.eemc.dao.DisciplineDocsDAO;
import net.gis.eemc.dao.DisciplineDocsDAOImpl;
import net.gis.eemc.domain.Discipline;
import net.gis.eemc.domain.Department;
import net.gis.eemc.domain.Faculty;
import net.gis.eemc.domain.Speciality;

/**
 *
 * @author Sergey
 */
public class DisciplineServiceImpl {
    DisciplineDAO disciplineDAO = null;
    DisciplineDocsDAO docsDAO = null;
    public final String gisStorage = "/home/vinser52/gis_storage/";
    public final String ftpLogin = "vinser52";
    public final String ftpPwd = "123123";
    public final String ftpHost = "localhost";
    public final int ftpPort = 21;

    public DisciplineServiceImpl(){
        disciplineDAO = new DisciplineDAOImpl();
        docsDAO = new DisciplineDocsDAOImpl();
    }

    public void saveDiscipline( Department dep,
            String code,
            String disciplineName,
            boolean lectures,
            boolean practice,
            boolean labs,
            boolean quiz,
            boolean exam,
            boolean courseProject,
            boolean test,
            List<Speciality> specialities) {
        String spec = new String();
        for (Speciality s : specialities) {
            spec.concat(spec.toString() + ",");
        }
        disciplineDAO.addDiscipline(new Discipline(dep, Integer.parseInt(code), disciplineName, lectures, practice, labs, quiz, exam, courseProject, test, spec));
    }
    
    public List<Discipline> getDisciplines() {
        return disciplineDAO.listDisciplines();
    }

    public List<Discipline> getDisciplines(Department department) {
        return disciplineDAO.listDisciplines(department);
    }

    public List<Discipline> getDisciplines(Faculty faculty) {
        return disciplineDAO.listDisciplines(faculty);
    }

    public Discipline getDiscipline(Integer discipId) {
        return disciplineDAO.getDiscipline(discipId);
    }

    public Boolean isSpecialityWithCodeExists(String code) {
        List<Discipline> disciplines = null;
        try {
            disciplines = disciplineDAO.getDisciplinesWithCode(Integer.parseInt(code));
            return !disciplines.isEmpty();
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
    }

    public void saveExcerptGos(UploadedFile uploadedFile, Discipline discipline) throws Exception{
        try {
            FtpClient ftp = new FtpClient(ftpHost, ftpPort);
            ftp.login(ftpLogin, ftpPwd);
            ftp.binary();
            ftp.cd(gisStorage);
            TelnetOutputStream tos = ftp.put(uploadedFile.getName());
            byte[] buffer = new byte[8 * 1024];
            InputStream inStream = uploadedFile.getInputStream();
            DataOutputStream output = new DataOutputStream(tos);

            try {

                int bytesRead;
                while ((bytesRead = inStream.read(buffer)) != -1) {
                    output.write(buffer, 0, bytesRead);
                }

            } finally {
                tos.flush();
                tos.close();
                inStream.close();
                output.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
